<?php
namespace Admin\Controller;
use Think\Controller;

class FangweiController extends Controller {
    public function addFujiaInfo(){
        $goods_id=I('get.goods_id');
        $cat_id=I('get.cat_id');
        $fields=M('field')->where("term_id={$cat_id}")->select();
        if ($fields) {
            
            $info=M('security_info_'.$cat_id)->where("goods_id={$goods_id}")->find();
            
            if($info){
                $info['checkbox'] = explode(',',$info['checkbox']);
                $this->assign('info',$info);
                $this->assign('act','update');  
            }else{
                $this->assign('act','add');
            }
            //$data=M('security_info_'.$cat_id)->where("goods_id={$goods_id}")->find();
            foreach($fields as $k =>$v){
                $fields[$k]['value']=explode("\r\n", $v['value']);
               
            }
            $this->assign('cat_id',$cat_id);
            $this->assign('goods_id',$goods_id);
            $this->assign('fields',$fields);
            $this->display();
        }else{
            $this -> error('请先到分类列表添加信息!');
        }
    }

    //具体信息  --提交表单
    public function addFujiaInfoSub(){
        $cat_id=I('post.cat_id');
        $checkbox_name=I('post.checkbox_name');
        $data=I('post.');
        unset($data['cat_id']);
        unset($data['checkbox_name']);
        $data[$checkbox_name]=implode(',',$data[$checkbox_name]);

        if ($_FILES['fujian']['name'] || $_FILES['pic']['name']) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =      ''; // 设置附件上传（子）目录
            if ($_FILES['pic']['name']) {
                // 上传单个文件 
                $info   =   $upload->uploadOne($_FILES['pic']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                     $data['pic'] = '/Uploads/'.$info['savepath'].$info['savename'];
                }
            }
            if ($_FILES['fujian']['name']) {
                // 上传单个文件 
                $info1   =   $upload->uploadOne($_FILES['fujian']);
                if(!$info1) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                     $data['fujian'] = '/Uploads/'.$info1['savepath'].$info1['savename'];
                }
            }
        }
        if ($data['act'] == 'add') {
            $res=M('security_info_'.$cat_id)->add($data);
        }else{
            $res=M('security_info_'.$cat_id) -> where(array('goods_id'=>$data['goods_id'])) ->save($data);
        }

        if ($res) {
            $this -> success('操作成功');
        }else{
            $this -> error('操作失败');
        }
    }

    public function addInfo()
    {
        $this -> display();
    }
}
